#include <iostream>
using namespace std;
// 用来记录活跃成员的标签
enum class RecordType
{
    Number,
    Name
};
union StudentUnion
{
    unsigned int number;
    char name[10];
};
struct Student
{
    RecordType type;
    StudentUnion data;
};
int main()
{
    Student u;
    // 赋值前设置标签
    u.type = RecordType::Number;
    u.data.number = 42;
    cout << u.data.number << endl; // OK，输出 42
    // 赋值前设置标签
    u.type = RecordType::Name;
    u.data.name[0] = 'A', u.data.name[1] = 'l', u.data.name[2] = '\0';
    cout << u.data.name << endl; // OK，输出 Al

    // 仅当活跃成员为 number 时才输出
    if (u.type == RecordType::Number)
    {
        cout << u.data.number << endl;
    }
}
